package cn.shyouchi.wuguanjia.android.ui.event.meet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.FutureTarget;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class PreviewMeetActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.edit_activity_name)
    TextView mEditActivityName;
    @BindView(R.id.edit_activity_position)
    TextView mEditActivityPosition;
    @BindView(R.id.edit_activity_person)
    TextView mEditActivityPerson;
    @BindView(R.id.textView5)
    TextView mTextView5;
    @BindView(R.id.edit_activity_content)
    TextView mEditActivityContent;
    @BindView(R.id.photo_snpl)
    BGASortableNinePhotoLayout mPhotoSnpl;
    @BindView(R.id.btn_del)
    Button mBtnDel;
    @BindView(R.id.btn_edit)
    Button mBtnEdit;
    @BindView(R.id.activity_add_handover)
    LinearLayout mActivityAddHandover;
    EventMeetEntity.ResultMessageBean entity;
    String projectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView() {
        return R.layout.activity_preview_meet;
    }

    @Override
    public void initInjector() {
        entity = getIntent().getParcelableExtra("meet");
        projectId = getIntent().getStringExtra("projectId");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());
        mPhotoSnpl.init(this);
        mEditActivityName.setText(entity.getService().getServicesname());
        mEditActivityPosition.setText(entity.getMeetingtime());
        mEditActivityPerson.setText(entity.getMeetingplace());
        mEditActivityContent.setText(entity.getMeetingcontent());
        covertImg(entity.getImage());
    }

    public void covertImg(List<String> urls) {
        Observable.from(urls)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, JSONObject>() {
                    @Override
                    public JSONObject call(String s) {
                        try {
                            return new JSONObject(s);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        throw new RuntimeException("Json转换失败");
                    }
                })
                .map(new Func1<JSONObject, String>() {
                    @Override
                    public String call(JSONObject json) {
                        try {
                            return Global.BASE_URL + json.getString("image");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        throw new RuntimeException("Json转换失败");
                    }
                })
                .flatMap(new Func1<String, Observable<String>>() {
                    @Override
                    public Observable<String> call(String url) {
                        return Observable.just(url)
                                .subscribeOn(Schedulers.io())
                                .map(new Func1<String, String>() {
                                    @Override
                                    public String call(String s) {
                                        FutureTarget<File> future = Glide
                                                .with(mContext)
                                                .load(s)
                                                .downloadOnly(500, 500);
                                        try {
                                            File file = future.get();
                                            String path = file.getAbsolutePath();
                                            return path;
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        } catch (ExecutionException e) {
                                            e.printStackTrace();
                                        }
                                        throw new RuntimeException("图片地址无效");
                                    }
                                });


                    }
                }).toList()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<String> photos) {
                        LogUtil.json(photos);
                        mPhotoSnpl.setData((ArrayList<String>) photos);
                        entity.setImage(photos);
                    }
                });


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @OnClick({R.id.btn_del, R.id.btn_edit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_del:
                EventApi mEventApi = new EventApi(mContext);
                mEventApi.delMeet(String.valueOf(entity.getId()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                            @Override
                            public void onNext(BaseRequestResult result) {
                                EventBus.getDefault().post(new DataChangeEvent());
                                finish();
                            }
                        });


                break;
            case R.id.btn_edit:

                Intent intent = new Intent(mContext, AddMeetActivity.class);

                intent.putExtra("meet", entity);
                intent.putExtra("projectId", projectId);
                startActivity(intent);
                finish();
                break;
        }
    }
}
